"""
网格搜索调参程序
"""
import os
import time
import itertools

lrs = [0.01]
regss = [0]

channels = [8]
n_factors = [4]
node_dropout_rates = [0.1]
mess_dropout_rates = [0.6]
dims = [64]

train_set = 'train'
test_set = 'test'
recall_set = 'recall_items_5w' # 切换这里之后，其实不用重新训练，只要评估就可以，但是要注意复制一份weight，修改log的id就可以了。还是很方便的。
kg = 'submit_kg_final_5'
dataset = 'submit_KGIN_data'
mode = 'train'

n_item = 195244
n_entity = 198821
n_node = 398821
n_relation = 2
gpu_id = 1

epoch = 30

model = 'KGIN_RI'

runed_id = 3
runing_id = []
max_run_num = 1
running_num = 0

for grid in itertools.product(lrs,channels,n_factors,regss,dims,node_dropout_rates,mess_dropout_rates):
    lr, channel, n_factor, regs, dim, node_dropout_rate, mess_dropout_rate = grid
    if running_num == max_run_num:
        while True:
            for run_id in runing_id:
                time.sleep(15)
                # logging.info('scanning %d.log......\n',run_id)
                for line in open(str(run_id)+'_'+dataset+'_'+model+'_'+mode+'.log'):
                    if 'Best Result=' in line:
                        running_num -= 1
                        runing_id.remove(run_id)

            if running_num != max_run_num:
                break

    os.system(' nohup python3.6 main.py --dataset '+dataset+' \
    --dim '+str(dim)+' --lr '+ str(lr) +'\
            --sim_regularity 0.0001 --batch_size 49152 --test_batch_size 2048 \
            --model '+model+' --n_factors '+str(n_factor)+'\
            --node_dropout True --test_set '+test_set+' --node_dropout_rate '+str(node_dropout_rate)+' \
            --mess_dropout True --channel '+str(channel)+'\
            --mess_dropout_rate '+str(mess_dropout_rate)+' \
            --gpu_id '+str(gpu_id)+' --cuda True --save_model_id '+str(runed_id)+'\
             --epoch '+str(epoch)+' --top_generate_setting include_train \
             --input_num True --n_item '+str(n_item)+' \
             --n_entity '+str(n_entity)+' --n_node '+str(n_node)+' \
             --n_relation '+str(n_relation)+' \
             --recall_set '+recall_set+' \
             --context_hops 2 --l2 '+str(regs)+'\
            --Ks [50] --mode '+mode+' --train_set '+train_set+' \
            --negative_sampling all \
            --stop_train_condition loss --kg '+kg+' \
            --loss_start_epoch 0 --rebuy_num 50'+ ' >' +\
              str(runed_id)+'_'+dataset+'_'+model+'_'+mode+'.log 2>&1 &')
    runing_id.append(runed_id)
    runed_id += 1
    running_num += 1


